package io.gbznt.modules.buss.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.gbznt.common.utils.PageUtils;
import io.gbznt.common.utils.Query;

import io.gbznt.modules.buss.dao.BussWaterpumDao;
import io.gbznt.modules.buss.entity.BussSwitchEntity;
import io.gbznt.modules.buss.entity.BussWaterpumEntity;
import io.gbznt.modules.buss.service.BussFarmlandInfoService;
import io.gbznt.modules.buss.service.BussSwitchService;
import io.gbznt.modules.buss.service.BussWaterpumService;


@Service("bussWaterpumService")
public class BussWaterpumServiceImpl extends ServiceImpl<BussWaterpumDao, BussWaterpumEntity> implements BussWaterpumService {
	@Autowired
	private BussSwitchService beservice;
	
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
    	String name = (String) params.get("name");
        IPage<BussWaterpumEntity> page = this.page(
                new Query<BussWaterpumEntity>().getPage(params),
                new QueryWrapper<BussWaterpumEntity>()
                .like(StringUtils.isNotBlank(name), "name", name)
        );
        for (BussWaterpumEntity busswater : page.getRecords()) {
        	QueryWrapper<BussSwitchEntity> nq = new QueryWrapper<>();
        	nq.apply("1=1 order by create_time DESC LIMIT 1");
			java.util.List<BussSwitchEntity> s =  beservice.list(nq);
			if(s.size()>0){
				BussSwitchEntity getbuss = s.get(0);
				//busswater.setSwitchb(getbuss.getSwitch1());
				//当前控制开关灯地址
				String statu = busswater.getSwitchn();
				String pum = busswater.getPumn();
				
				String statuname = name(statu, getbuss);
				String pumname = name(pum, getbuss);
				if(statuname.equals("1")){
					statuname = "已开启";
				}else{
					statuname = "已关闭";
				}
				if(pumname.equals("1")){
					pumname = "已开启";
				}else{
					pumname = "已关闭";
				}
				
				busswater.setSwitchnnew(statuname);
				busswater.setPumnnew(pumname);
				
			}
			
		}

        return new PageUtils(page);
    }
    
    public String name(String statu,BussSwitchEntity getbuss) {
    	String getstring = "";
    	if(statu.equals("1")){
    		getstring = getbuss.getSwitch1().toString();
		}else if(statu.equals("2")){
			getstring = getbuss.getSwitch2().toString();
		}else if(statu.equals("3")){
			getstring = getbuss.getSwitch3().toString();
		}else if(statu.equals("4")){
			getstring = getbuss.getSwitch4().toString();
		}else if(statu.equals("5")){
			getstring = getbuss.getSwitch5().toString();
		}else if(statu.equals("6")){
			getstring = getbuss.getSwitch6().toString();
		}else if(statu.equals("7")){
			getstring = getbuss.getSwitch7().toString();
		}else if(statu.equals("8")){
			getstring = getbuss.getSwitch8().toString();
		}else if(statu.equals("9")){
			getstring = getbuss.getSwitch9().toString();
		}else if(statu.equals("10")){
			getstring = getbuss.getSwitch10().toString();
		}else if(statu.equals("11")){
			getstring = getbuss.getSwitch11().toString();
		}else if(statu.equals("12")){
			getstring = getbuss.getSwitch12().toString();
		}else if(statu.equals("13")){
			getstring = getbuss.getSwitch13().toString();
		}else if(statu.equals("14")){
			getstring = getbuss.getSwitch14().toString();
		}else if(statu.equals("15")){
			getstring = getbuss.getSwitch15().toString();
		}else if(statu.equals("16")){
			getstring = getbuss.getSwitch16().toString();
		}else if(statu.equals("17")){
			getstring = getbuss.getSwitch17().toString();
		}else if(statu.equals("18")){
			getstring = getbuss.getSwitch18().toString();
		}else if(statu.equals("19")){
			getstring = getbuss.getSwitch19().toString();
		}else if(statu.equals("20")){
			getstring = getbuss.getSwitch20().toString();
		}
    	return getstring;
		
	}

}